package com.httpdemo.http_teaching_server.controller;

import com.httpdemo.http_teaching_server.model.User;
import com.httpdemo.http_teaching_server.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/auth")
public class UserRegisterController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/register")
    public ResponseEntity<Map<String, String>> register(@RequestBody Map<String, String> body) {
        String username = body.get("username");
        String password = body.get("password");

        if (userRepository.existsByUsername(username)) {
            return ResponseEntity.badRequest().body(Map.of("error", "用户名已存在"));
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password); // 在实际应用中，密码应加密存储
        userRepository.save(user);

        return ResponseEntity.ok(Map.of("message", "注册成功"));
    }
}